package com.zly.framework.rbac.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zly.framework.rbac.model.entity.UserRole;
import com.zly.framework.rbac.mapper.UserRoleMapper;
import com.zly.framework.rbac.service.UserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    /**
     * 删除sys_user_role 表中的userid
     *
     * @param userIds 用户ids
     */
    @Override
    public void deleteUserRoleByUserIds(List<Long> userIds) {
        this.baseMapper.deleteBatchIds(userIds);
    }

    /**
     * 分配用户角色
     *
     * @param userIds userIds
     * @param roleIds roleIds
     */
    @Transactional
    @Override
    public void grantUserRoleByUserIds(List<Long> userIds, List<Long> roleIds) {
        ArrayList<UserRole> userRoles = new ArrayList<>();
        for (Long userId : userIds) {
            for (Long roleId : roleIds) {
                UserRole userRole = new UserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(roleId);
                userRoles.add(userRole);
            }
        }
        this.saveBatch(userRoles);
    }
}
